package Test;

import java.io.IOException;

import Interact.Interactable;
import Interact.Interface;
import Test.Exception.FactoryException;
import Tool.ObjectFactory;

abstract public class Question implements Interactable{
	public static final int ANSWER_MODE = 0;
	public static final int CORRECT_MODE = 1;
	
	/** 工厂类 */
	public static final ObjectFactory<Question> factory = 
		new ObjectFactory<Question>();
	static{
		try {
			factory.updateClassMap(Judge.TYPE, Judge.class, "判断题");
			factory.updateClassMap(App.TYPE, App.class, "应用题");
			factory.updateClassMap(Selection.TYPE, Selection.class, "单选题");
		} catch (FactoryException e) {
			System.err.println("注册失败");
			e.printStackTrace();
		}
	}
	
	public Question(){
		initModes();
	}
	
	/** 题面 */
	protected QuestionBody body;
	
	/** 内容，包括题号、答案等 */
	protected Content content;
	
	/**
	 * 自动评分
	 * 复写时要求设置Content的score选项
	 * @return -1表示默认无法评分，同时设置score = -1
	 */
	public int correct(){
		content.setScore(-1);
		return content.getScore();
	}
	
	/**
	 * 获取类型
	 */
	abstract public int getType();
	
	@Override
	public String getKey() {
		return body.getId() + "and" + body.getCourse().getId();
	}
	
	protected int mode = ANSWER_MODE;
	
	@Override
	public void setMode(int mode) {
		this.mode = mode;
	}
	
	@Override
	public void display(Interface inf) throws IOException {
		if (modes == null || mode < 0 || mode >= modes.length || modes[mode] == null){
			System.err.println("hello world");
			System.err.println(getClass());
			System.err.println("mode越界或modes[mode]未设置");
			return;
		}
		modes[mode].display(inf);
	}
	
	@Override
	public void setValueViaInterface(Interface inf) {
		if (modes == null || mode < 0 || mode >= modes.length || modes[mode] == null){
			System.err.println("hello world");
			System.err.println(getClass());
			System.err.println("mode越界或modes[mode]未设置");
			return;
		}
		modes[mode].setValueViaInterface(inf);
	}
	
	protected InteractMode modes[];
	
	abstract void initModes();

	/** getters and setters */
	public QuestionBody getBody() {
		return body;
	}

	public void setBody(QuestionBody body) {
		this.body = body;
	}

	public Content getContent() {
		return content;
	}

	public void setContent(Content content) {
		this.content = content;
	}
}


interface InteractMode{
	void display(Interface inf) throws IOException;
	void setValueViaInterface(Interface inf);
}